import { defineStore } from "pinia"
import { ComputeTaskOverview, ComputeTaskPageResult, ComputeTaskPageSearch } from "@/models/compute-task"
import { computeTaskApi } from "@/services"
import { toast } from "vue3-toastify"

const useComputeTaskStore  = defineStore('ComputeTask', {
    // state
    state:() =>({
        status: '',    // 任务状态
        startDate: '', // 开始日期
        endDate: '',   // 结束日期
        pageSize: 10,
        pageNum: 1,
        totalRecords:0,
        overviewResult: {} as ComputeTaskOverview,
        computeTaskPageResult: {} as ComputeTaskPageResult,
    }),
    // actions
    actions:{
        async getComputeTaskOverview() {
            try{
                const res = await computeTaskApi.getComputeTaskOverview()
                this.overviewResult = res ? res : {} as ComputeTaskOverview
            } catch(err) {
                toast.error("获取我的计算作业概览失败")
            }
        },
        async getComputeTasks(status:string, startDate:string, endDate: string, pageSize:number, pageNum:number) {
            try {
                const data : ComputeTaskPageSearch = {
                    status: status,
                    startTime: this.getStartTime(startDate),
                    endTime: this.getEndTime(endDate),
                    pageSize: pageSize,
                    pageNum: pageNum,
                }
                const res = await computeTaskApi.getComputeTask(data)
                this.computeTaskPageResult = res ? res : {} as ComputeTaskPageResult
                this.totalRecords = this.computeTaskPageResult.count
            } catch(err) {
                toast.error("获取我的计算作业失败")
            }
        },
        async getMyCurrentList() {
            try {
                const res = await computeTaskApi.getMyCurrentList()
                this.computeTaskPageResult = res ? res : {} as ComputeTaskPageResult
                this.totalRecords = this.computeTaskPageResult.count
            } catch(err) {
                toast.error("获取我的当前计算作业失败")
            }
        },

        // 初始化数据
        async initData(){
            await this.getComputeTaskOverview();
            await this.getMyCurrentList();
        },

        // 刷新数据（在当前页）
        async refreshData(){
            await this.getComputeTasks(this.status, this.startDate, this.endDate, this.pageSize, this.pageNum)
        },

        // 开始查询
        async handleSearch(){      
            await this.getComputeTasks(this.status, this.startDate, this.endDate, this.pageSize, 1)
        },

        async handlePageChange(pageNum:number,pageSize:number){     
            this.pageNum = pageNum
            this.pageSize = pageSize   
            this.refreshData()
        },

        getStartTime(startDate:string) {
            return startDate ? `${startDate} 00:00:00` : '';
        },

        getEndTime(endDate:string) {
            return endDate ? `${endDate} 23:59:59` : '';
        },
    },
    getters:{
        overviewSource:(state) => state.overviewResult,
        computeTaskSource:(state) => state.computeTaskPageResult.records,
    }
})

export default useComputeTaskStore